package com.tianxiao.faas.application.web.corner;

import com.tianxiao.faas.biz.infrastructure.facade.ObjectStoreFacade;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import com.tianxiao.faas.common.util.ImageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("object/user")
public class ObjectUploadController {
    static final Logger logger = LoggerFactory.getLogger(ObjectStoreFacade.class);
    @Resource
    private ObjectStoreFacade objectStoreFacade;

    @PostMapping("/upload")
    @Access
    public Result<Object> upload(@RequestParam("file") MultipartFile file) {
        String url = "";
        try {
            String originalFilename = file.getOriginalFilename();
            if (!ImageUtils.isImage(originalFilename) && !ImageUtils.isMov(originalFilename)) {
                return Result.builder().success(false).msg("上传文件格式不符");
            }
            url = objectStoreFacade.staticResourceUpload(file.getInputStream(), originalFilename);
        } catch (IOException e) {
            logger.error("",e);
        }
        return Result.builder().data(url).success(true);
    }
}
